package com.android.porttallymansystem.ui.adapter

import android.content.Intent
import android.text.Layout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.android.porttallymansystem.R
import com.android.porttallymansystem.logic.model.BriefMessage
import com.android.porttallymansystem.ui.activity.PersonMessageActivity
import de.hdodenhof.circleimageview.CircleImageView

class MessageAdapter(private val messageList: List<BriefMessage>) : RecyclerView.Adapter<MessageAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val profile: CircleImageView = view.findViewById(R.id.civ_brief_message_profile)
        val name: TextView = view.findViewById(R.id.tv_brief_message_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_brief_message, parent, false)
        val holder = ViewHolder(view)
        holder.itemView.setOnClickListener {
            val intent = Intent(parent.context, PersonMessageActivity::class.java)
            intent.putExtra("person_id", messageList[holder.adapterPosition].id)
            parent.context.startActivity(intent)
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.profile.setImageResource(messageList[position].profile)
        holder.name.text = messageList[position].name
    }

    override fun getItemCount() = messageList.size
}